
import static org.junit.Assert.*;

import java.math.BigDecimal;
import java.math.MathContext;

import org.junit.*;


public class TestTemperature {

	Temperature tempK1;
	Temperature tempC1;
	Temperature tempC2;
	Temperature tempC3;
	Temperature tempF1;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
		tempK1 = new Temperature(new BigDecimal(200), TemperatureUnit.KELVIN);
		tempC1 = Temperature.CELSIUS_0;
		tempC2 = new Temperature(BigDecimal.ZERO, TemperatureUnit.CELSIUS);
		tempC3 = new Temperature(new BigDecimal(200), TemperatureUnit.CELSIUS);
		tempF1 = new Temperature(new BigDecimal(200), TemperatureUnit.FAHRENHEIT);
	}

	@After
	public void tearDown() throws Exception {
	}
	
	@Test
	public void constructExpanded(){
		assertEquals(tempK1.getNumeral().intValue(), new BigDecimal(200).intValue());
	}
	
	@Test
	public void constructShort(){
		assertEquals(tempC1.getNumeral().intValue(), tempC2.getNumeral().intValue());
	}
	
	@Test
	public void convertionFromCToF(){
		assertEquals((tempC3.toTemperatureUnit(TemperatureUnit.FAHRENHEIT)).getNumeral().round(context3BC).intValue(), 
				new BigDecimal(392).intValue());
	}
	
	@Test
	public void convertionFromCToK(){
		assertEquals((tempC3.toTemperatureUnit(TemperatureUnit.KELVIN)).getNumeral().round(context3BC).intValue(), 
				new BigDecimal(473).intValue());
	}
	
	@Test
	public void convertionFromCToC(){
		assertEquals((tempC3.toTemperatureUnit(TemperatureUnit.CELSIUS)).getNumeral().round(context3BC).intValue(), 
				new BigDecimal(200).intValue());
	}
	
	@Test
	public void convertionFromFToC(){
		BigDecimal i1 = (tempF1.toTemperatureUnit(TemperatureUnit.CELSIUS)).getNumeral();
		assertEquals(i1.round(context2BC).intValue(), new BigDecimal(93).intValue());
	}
	
	@Test
	public void convertionFromFToK(){
		BigDecimal i2 = (tempF1.toTemperatureUnit(TemperatureUnit.KELVIN)).getNumeral();
		assertEquals(i2.round(context3BC).intValue(), new BigDecimal(366).intValue());
	}
	
	@Test
	public void convertionFromFToF(){
		assertEquals((tempF1.toTemperatureUnit(TemperatureUnit.FAHRENHEIT)).getNumeral().round(context3BC).intValue(), 
				new BigDecimal(200).intValue());
	}
	
	@Test
	public void convertionFromKToC(){
		BigDecimal i3 = (tempK1.toTemperatureUnit(TemperatureUnit.CELSIUS)).getNumeral();
		assertEquals(i3.intValue(), new BigDecimal(-73).intValue());
	}
	
	@Test
	public void convertionFromKToF(){
		BigDecimal i4 = (tempK1.toTemperatureUnit(TemperatureUnit.FAHRENHEIT)).getNumeral();
		assertEquals(i4.round(context2BC).intValue(), new BigDecimal(-100).intValue());
	}
	
	@Test
	public void convertionFromKToK(){
		assertEquals((tempK1.toTemperatureUnit(TemperatureUnit.KELVIN)).getNumeral().intValue(), 
				new BigDecimal(200).intValue());
	}
	
	@Test
	public void reconversion1(){
		assertEquals(tempK1.getNumeral().intValue(),
				tempK1.toTemperatureUnit(TemperatureUnit.CELSIUS).toTemperatureUnit(TemperatureUnit.KELVIN).getNumeral().intValue());
	}
	
	@Test
	public void reconversion2(){
		assertEquals(tempK1.getNumeral().intValue(),
				tempK1.toTemperatureUnit(TemperatureUnit.FAHRENHEIT).toTemperatureUnit(TemperatureUnit.KELVIN).getNumeral().round(context3BC).intValue());
	}
	
	@Test
	public void rounding(){
		assert true;
	}
	
	@Test
	public void roundingMultipleTimesK(){
		Temperature k1 = tempK1.toTemperatureUnit(TemperatureUnit.FAHRENHEIT);
		Temperature k2 = k1.toTemperatureUnit(TemperatureUnit.CELSIUS);
		Temperature k3 = k2.toTemperatureUnit(TemperatureUnit.KELVIN);
		assertEquals(tempK1.getNumeral().intValue(),k3.getNumeral().round(context3BC).intValue());
	}
	
	@Test
	public void roundingMultipleTimesC(){
		Temperature c1 = tempC3.toTemperatureUnit(TemperatureUnit.KELVIN);
		Temperature c2 = c1.toTemperatureUnit(TemperatureUnit.FAHRENHEIT);
		Temperature c3 = c2.toTemperatureUnit(TemperatureUnit.CELSIUS);
		assertEquals(tempC3.getNumeral().intValue(),c3.getNumeral().intValue());
	}
	
	@Test
	public void roundingMultipleTimesF(){
		Temperature f1 = tempF1.toTemperatureUnit(TemperatureUnit.CELSIUS);
		Temperature f2 = f1.toTemperatureUnit(TemperatureUnit.KELVIN);
		Temperature f3 = f2.toTemperatureUnit(TemperatureUnit.FAHRENHEIT);
		assertEquals(tempF1.getNumeral().intValue(),f3.getNumeral().intValue());
	}
	
	@Test
	public void comparingSame(){
		assertEquals(tempC2.compareTo(tempC1), 0);
	}
	
	@Test
	public void comparingDifferent(){
		assertEquals(tempC2.compareTo(tempC3), -1);
	}
	
	@Test(expected=ClassCastException.class) 
	public void comparingFault() throws Exception{
		tempC3.compareTo(tempK1);
	}
	
	@Test
	public void sameValueTest() {
		assertFalse(tempC3.hasSameValueAs(tempK1));
	}
	
	MathContext context2BC = new MathContext(2);
	MathContext context3BC = new MathContext(3);
}
